import { _decorator, Component } from 'cc';
import * as cc from 'cc';
import SceneBase from '../../lib/scene/SceneBase';
const { ccclass, property } = _decorator;

/**
 * 热更新 场景
 * @description 交流群：415468592
 */
@ccclass('UpdateScene')
export default class UpdateScene extends SceneBase {

    @property({ type: cc.Label, displayName: "热更进度" })
    public lbl_percent: cc.Label = null;

    onLoad() {
        cc.director.on('HotUpdateFinish', this.onHotUpdateFinish, this);
        cc.director.on('HotUpdateRate', this.onHotUpdateRate, this);
    }
    start() {
        if (!cc.sys.isNative) {
            // cc.director.preloadScene('Game');
            // cc.director.preloadScene('Hall');
            this.scheduleOnce(() => {
                this.goHallCall();
            }, 0.5)
        }
    }

    /**
    * 跳转大厅
    */
    private goHallCall() {
        cc.director.loadScene("Hall");
    }

    /**
    * 跳转大厅
    */
    private onUpdateFinish() {
        console.log("热更新完成");
        this.goHallCall();
    }

    /**
     * 热更完成回调
     * @param param 
     */
    private onHotUpdateFinish(param: any) {
        let result = param;
        if (result) {
            this.onUpdateFinish();
        } else {
            this.onUpdateFinish();
        }
    }

    /**
     * 热更进度
     * @param param 进度
     */
    private onHotUpdateRate(param: any) {
        let percent = Number(param);
        if (percent > 1) percent = 1;
        console.log("热更进度：" + percent);
        this.lbl_percent.string = '正在更新游戏资源，更新进度' + (percent * 100) + '%';
    }

    onDisable() {
        cc.director.off('HotUpdateFinish', this.onHotUpdateFinish, this);
        cc.director.off('HotUpdateRate', this.onHotUpdateRate, this);
    }
}